package com.tzxs.pay.main;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import static android.content.Context.INPUT_METHOD_SERVICE;

/**
 * Created by zfc on 2018/3/29.
 */
public abstract class BaseFragment<T extends ViewDataBinding> extends Fragment {
    public T binding;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, getContentLayoutId(), container, false);
        //点击隐藏键盘
        binding.getRoot().setOnClickListener(v -> {
            Context context = getContext();
            if (context == null) {
                return;
            }
            if (null != getActivity().getCurrentFocus()) {
                /**
                 * 点击空白位置 隐藏软键盘
                 */
                InputMethodManager mInputMethodManager = (InputMethodManager) (context.getSystemService(INPUT_METHOD_SERVICE));
                if (mInputMethodManager != null)
                    mInputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
            }
        });
        return binding.getRoot();
    }

    //获取到fragment的布局id
    public abstract int getContentLayoutId();

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewCreate(savedInstanceState);
    }

    protected abstract void viewCreate(Bundle savedInstanceState);
}